<?php
//error_reporting(E_ALL);
//ini_set('display_errors', '1');

$conf['cron_safe_threshold'] = 1;

@session_start();
include_once '/home/u172676809/public_html/session_var.php';
include_once '/home/u172676809/public_html/db_conecta_adodb.inc.php';

     

//Consume la cotizacion y actualiza los registros

//$db->debug=true;

//Busco las cotizaciones existentes

try{
	$rs=sql('SELECT T.ID_TRADE AS ID_TRADE,C.ID_CURRENCY ID_TO,C.CHR_CODE AS "TO",C2.ID_CURRENCY ID_FROM, C2.CHR_CODE AS "FROM" 
			FROM u172676809_fcac.T_TRADE T, u172676809_fcac.T_CURRENCY C, u172676809_fcac.T_CURRENCY C2
			WHERE T.ID_CURRENCY = C.ID_CURRENCY
			AND T.ON_CURRENCY = C2.ID_CURRENCY','');
	while($row=siguiente($rs)){
		$url='https://www.google.com/finance/converter?a=1&from='.$row->FROM.'&to='.$row->TO;
		$result = file_get_contents($url);
		$result = explode("<span class=bld>",$result);
		$result = explode("</span>",$result[1]); 
		$cotizacion = preg_replace("/[^0-9\.]/", null, $result[0]);
		$cotizacion=round(floatval($cotizacion),4);

		//guardo cotizacion historica verificando si ya existe para ese día
		$rs_exists=sql("SELECT * FROM u172676809_fcac.T_HR_TRADE WHERE ON_CURRENCY=? AND ID_CURRENCY=? AND DT_DATE=DATE_FORMAT(DATE_SUB(SYSDATE(),INTERVAL 1 DAY),'%Y-%m-%d')",array($row->ID_FROM,$row->ID_TO));
		if ($rs_exists->RecordCount() == 0){
			sql("INSERT INTO u172676809_fcac.T_HR_TRADE 
				(SELECT NULL,ID_CURRENCY,DCM_VALUE,ON_CURRENCY,DATE_SUB(SYSDATE(),INTERVAL 1 DAY) 
					FROM u172676809_fcac.T_TRADE 
					WHERE ON_CURRENCY=? AND ID_CURRENCY=?)",array($row->ID_FROM,$row->ID_TO));
		}
		//GUARDO LA COTIZACION DEL DIA
		sql("UPDATE u172676809_fcac.T_TRADE SET DCM_VALUE=? WHERE ID_CURRENCY=? AND ON_CURRENCY=?",array($cotizacion,$row->ID_TO,$row->ID_FROM));
	}
}catch (Exception $e) {
    //Guardo el error en novedades
    setSysNew('e','El proceso de actualizacion de la cotizacion diaria ha fallado');
    return false;
}
setSysNew('o','Actualizacion de Cotizacion del '.date('d/m/Y H:i:s'). ' hs');
return true;